package com.tuoyu.peopletest.utils;

import static android.os.Build.MANUFACTURER;
import static android.os.Build.MODEL;
import static com.tuoyu.peopletest.common.MyApplication.context;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import java.io.File;

public class DeviceUtils {
    //获取SD卡大小
    public static String getAllSDSize(Context context) {
        try {
            //首先指定需要获取大小的目录
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();

            //得到全部区块
            long availableBlocks = stat.getBlockCount();

            long availsd = blockSize * availableBlocks;
            return Formatter.formatFileSize(context, availsd);
        } catch (Exception e) {
        }
        return "N/A";
    }
    public static String getMemoryInfo(Context context) {
        //获取运行内存的信息
        ActivityManager manager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
        manager.getMemoryInfo(info);
//        StringBuilder sb = new StringBuilder();
//        sb.append("可用RAM:");
//        sb.append(info.availMem + "B");
//        sb.append(",总RAM:");
//        sb.append(info.totalMem + "B");
//        sb.append("\r\n");
//        sb.append(Formatter.formatFileSize(context, info.availMem));
//        sb.append(",");
//        sb.append(Formatter.formatFileSize(context, info.totalMem));
//        return sb.toString();
        return Formatter.formatFileSize(context, info.totalMem);
    }
    public static String getDeviceInfo(Context context){
        String device=MANUFACTURER + " " + MODEL;
        String version="Android " + Build.VERSION.RELEASE;

        return device+" "+version+" "+getScreenInfo(context)+" "+getMemoryInfo(context)+" "+getAllSDSize(context);
    }
    public static String getScreenInfo(Context context){
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        String density = String.valueOf(dm.density); // 屏幕密度（0.75 / 1.0 / 1.5 / 2.0）
        String resolution = String.valueOf(dm.heightPixels) + "*" + String.valueOf(dm.widthPixels);
        return resolution;
    }
}
